/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "BackintTool.h"
#include "common/File.h"
#include "ParseParamVOneFive.h"

using namespace backint;

namespace {
const mp_string PARAM_PATH = "/usr/sap/";
}

BackintTool::BackintTool(const std::string version)
{
    if (version == "1.5" || version == "1.0") {
        m_parseParamCls = std::make_shared<ParseParamVOneFive>("");
    }
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("backup",
        std::bind(&BackintTool::BackupMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("restore",
        std::bind(&BackintTool::RestoreMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("inquire",
        std::bind(&BackintTool::InquireMain, this, std::placeholders::_1)));
    m_backintFunMap.insert(std::make_pair<std::string, BackintFunc>("delete",
        std::bind(&BackintTool::DeleteMain, this, std::placeholders::_1)));
}

uint32_t BackintTool::Run(int argc, char **argv)
{
    LOGGUARD("");
    if (m_parseParamCls->ParamParse(argc, argv) != SUCCESS) {
        ERRLOG("Param parse fialed.");
        return FAILED;
    }
    StuParam tmpParam = m_parseParamCls->GetParam();
    if (GetDbNameFromInfile(tmpParam.m_inputFile, m_dbName) != SUCCESS) {
        ERRLOG("Get db name failed.");
        return FAILED;
    }
    INFOLOG("Start run function: %s.", tmpParam.m_funType.c_str());
    auto iter = m_backintFunMap.find(tmpParam.m_funType);
    if (iter != m_backintFunMap.end()) {
        return iter->second(tmpParam);
    }
    ERRLOG("Unsupported operation %s.", tmpParam.m_funType.c_str());
    return FAILED;
}

uint32_t BackintTool::BackupMain(const StuParam &inParam)
{
    LOGGUARD("");
    StuFileInfo stFileInfo;
    if (ReadPipeInfo(inParam.m_inputFile, stFileInfo) != SUCCESS) {
        ERRLOG("Failed to read pipe info.");
        return FAILED;
    }
    std::string paramFileName = "saphana_backup_" + m_dbName + "_opbackint_param.txt";
    std::string paramFile = PARAM_PATH + paramFileName;
    std::string fileNm = GetFileName(stFileInfo.fileData[0].m_filePath);
    INFOLOG("Get FileName is : %s. paramFile: %s.", fileNm.c_str(), paramFile.c_str());

    if (fileNm.find("databackup") != std::string::npos || Module::CFile::FileExist(paramFile.c_str())) {
        INFOLOG("Start parse plugin param for data backup.");
        if (ParsePluginParams(paramFile, m_plugParams) != SUCCESS) {
            ERRLOG("Failed to get plugin params.");
            return FAILED;
        }
    } else {
        INFOLOG("Start log backup");
        m_plugParams.m_cachePath = PARAM_PATH;
        std::string logParentPath;
        std::string parFile = GetExeCurrentPath() + "/hdbconfig/backint.conf";
        if (GetLogParentPath(parFile, logParentPath) != SUCCESS) {
            ERRLOG("Failed to get log path.");
            return FAILED;
        }
        std::string dbName;
        std::string filePh = inParam.m_inputFile;
        if (GetDbNameFromInfile(filePh, dbName) != SUCCESS) {
            ERRLOG("Get db name failed.");
            return FAILED;
        }
        std::string logParentPathDir = logParentPath + "/" + dbName;
        if (CreateDirs(logParentPathDir) != SUCCESS) {
            ERRLOG("Create backup id dir failed:%s.", logParentPathDir.c_str());
            return FAILED;
        }
        m_plugParams.m_pid = "0";
        m_plugParams.m_dbName = dbName;
        m_plugParams.m_dataPath = logParentPathDir;
        INFOLOG("logParentPath: %s", logParentPathDir.c_str());
    }
    m_backupHanlder = std::make_shared<BackupHandler>();
    return m_backupHanlder->ExecBackup(inParam, m_plugParams);
}

uint32_t BackintTool::RestoreMain(const StuParam &inParam)
{
    LOGGUARD("");
    std::string paramFileName = "saphana_recovery_" + m_dbName + "_opbackint_param.txt";
    std::string paramFile = PARAM_PATH + paramFileName;
    if (ParsePluginParams(paramFile, m_plugParams) != SUCCESS) {
        ERRLOG("Failed to get plugin params.");
        return FAILED;
    }
    m_restoreHanlder = std::make_shared<RestoreHandler>();
    return m_restoreHanlder->ExecRestore(inParam, m_plugParams);
}

uint32_t BackintTool::InquireMain(const StuParam &inParam)
{
    LOGGUARD("");
    std::string paramFileName = "saphana_recovery_" + m_dbName + "_opbackint_param.txt";
    std::string paramFile = PARAM_PATH + paramFileName;
    if (ParsePluginParams(paramFile, m_plugParams) != SUCCESS) {
        ERRLOG("Failed to get plugin params.");
        return FAILED;
    }
    m_inquireHanlder = std::make_shared<InquireHandler>();
    return m_inquireHanlder->ExecInquire(inParam, m_plugParams);
}

uint32_t BackintTool::DeleteMain(const StuParam &inParam)
{
    LOGGUARD("");
    return SUCCESS;
}